import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Sort66 {
    public  void insertSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if (array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    public  void shellSort(int[] array){
        int gap= array.length;
        while (gap>=1){
            shell(array,gap);
            gap=gap/2;
        }
    }
    public static void shell(int[] array,int gap) {
        for (int i = 0; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int tmp=i;
            for (int j = i+1; j <array.length ; j++) {
                if (array[tmp] > array[j]) {
                    tmp = j;
                }
            }
            int swap=array[tmp];
            array[tmp]=array[i];
            array[i]=swap;
        }
    }


public static void swap(int[] array ,int i,int j){
        int tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
}
public static void ShiftDown(int[] array,int parent,int len){
        int child =2*parent+1;
        while (child<len){
            if (child+1<len&&array[child]<array[child+1]){
                child++;
            }
            if (array[child]>array[parent]){
                swap(array,parent,child);
                parent=child;
                child=2*parent+1;
            }
        }
}

    public static void creatHeao(int[] array){
        for (int parent=(array.length-1-1)/2;parent>=0;parent--){
            ShiftDown(array,parent,array.length);
        }
}
   public  void heapSort(int[] array){
        int end=array.length-1;
        creatHeao(array);
        while (end>0) {
            swap(array, 0, end);
            ShiftDown(array, 0, end);
            end--;
        }
    }
public static void bubbleSort(int[] array){
    for (int i = 0; i < array.length-1 ; i++) {
        boolean flg = false;
        for (int j = 0; j < array.length-1-i ; j++) {
            if (array[j]>array[j+1]){
                swap(array,j,j+1);
                flg=true;
            }
        }
        if (!flg){
            break;
        }
    }
    }

    public  void quickSort(int[] array){
        int left=0;
        int right = array.length-1;
        quick(array,left,right);
    }
    public static void quick(int[] array,int start,int end){
        if(start>=end){
            return;
        }
        int qviot=partition(array,start,end);
        partition(array,start,qviot-1);
        partition(array,qviot+1,end);
    }
    public static int partition(int[] array,int left,int right){
        int tmp=array[left];
        while (left<right){
            while (left<right&&array[right]>=tmp){
                right--;
            }
            array[left]=array[right];
            while (left<right&&array[left]<=tmp){
                left++;
            }
            array[right]=array[left];
        }
        array[left]=tmp;
        return left;
    }
    public class PriorityQueue {
        public int[] elem;
        public int usedSize;

        public PriorityQueue(int[] array) {
            this.elem=Arrays.copyOf(array,array.length);
            this.usedSize=array.length;
        }


        public void createHeap(int[] array) {
            for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
                shiftDown(parent,array.length-1);
            }
        }


        private void shiftDown(int root,int len) {
            int child = 2*root+1;
            while (child<len){
                while (child+1<len&&elem[child]<elem[child+1]){
                    child++;
                }
                while (elem[child]>elem[root]){
                    swap(elem,child,root);
                    root=child;
                    child=2*root+1;
                }
            }
        }



        public void push(int val) {
            if (isFull()){
                elem= Arrays.copyOf(elem,elem.length*2);
            }
            elem[usedSize]=val;
            usedSize++;
            shiftUp(usedSize-1);
        }

        private void shiftUp(int child) {
            int parent = (child-1)/2;
            while (child>0){
                if (elem[parent]>elem[child]){
                    break;
                }else {
                    swap(elem,parent,child);
                    child=parent;
                    parent=(child-1)/2;
                }
            }
        }

        public boolean isFull() {
            return usedSize==elem.length;
        }


        public int pollHeap() {
            if (isEmpty()){
                return -1;
            }
            int old=elem[0];
            swap(elem,0,usedSize-1);
            usedSize--;
            shiftDown(0,usedSize);
            return old;
        }

        public boolean isEmpty() {
            return usedSize==0;
        }


        public int peekHeap() {
            return elem[0];
        }
    }
    public int[] smallestK (int[] arr, int k) {
        int[] elem=new int[0];
        java.util.PriorityQueue<Integer> priorityQueue = new java.util.PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }
        for (int i = 0; i <= k; i++) {
            int val=priorityQueue.poll();
            elem[i]=val;
            System.out.println(val);
        }

        return elem;
    }
 /*   public class BinaryTree {

         class TreeNode {
            public int val;
            public TreeNode left;//左孩子的引用
            public TreeNode right;//右孩子的引用

            public TreeNode(int val) {
                this.val = val;
            }
        }

        public TreeNode root;

        public void createTree() {
             TreeNode A = new TreeNode(1);
            TreeNode B = new TreeNode(2);
            TreeNode C = new TreeNode(3);
            TreeNode D = new TreeNode(4);
            TreeNode E = new TreeNode(5);
            TreeNode F = new TreeNode(6);

            A.left=B;
            A.right=D;
            B.left=C;
            D.left=E;
            D.right=F;
            this.root=A;

        }

        public void preOrder(TreeNode root) {
            if (root==null){
                return;
            }
            System.out.println(root.val);
            preOrder(root.left);
            preOrder(root.right);
        }
        void inOrder(TreeNode root) {
            if (root==null){
                return;
            }
            inOrder(root.left);
            System.out.println(root.val);
            inOrder(root.right);
        }

        void postOrder(TreeNode root) {
            if (root==null){
                return;
            }
            postOrder(root.left);
            System.out.println(root.val);
            postOrder(root.right);
        }

        public  int nodeSize;


        int size(TreeNode root) {
            if (root==null){
                return 0;
            }
            return size(root.left)+size(root.right)+1;
        }


        void size2(TreeNode root) {
            if (root==null){
                return ;
            }
            nodeSize++;
            size2(root.left);
            size2(root.right);
        }



        public  int leafSize = 0;

        int getLeafNodeCount1(TreeNode root) {
            if (root==null){
                return 0;
            }
            if (root.left==null&&root.right==null){
                return 1;
            }
            return getLeafNodeCount1(root.left)+getLeafNodeCount1(root.right);
        }


        void getLeafNodeCount2(TreeNode root) {
            if (root==null){
                return ;
            }
            if (root.left==null&&root.right==null){
                leafSize++;
            }
            getLeafNodeCount2(root.left);
            getLeafNodeCount2(root.right);
            System.out.println(leafSize);
        }


        int getKLevelNodeCount(TreeNode root, int k) {

        }


        int getHeight(TreeNode root) {

        }



        TreeNode find(TreeNode root, char val) {

            return null;
        }


        void levelOrder(TreeNode root) {

        }



        boolean isCompleteTree(TreeNode root) {
            return true;
        }
    }*/
    public  void mergeSort(int[] array){
        merge(array,0,array.length-1);
    }
    public static void merge(int[] array,int left,int right){
        if (left>=right){
            return;
        }
        int midIndex=(left+right)/2;
        merge(array,left,midIndex);
        merge(array,midIndex+1,right);
        mergerSort(array,left,right,midIndex);

    }
    public void mergeSort2(int[] array){
        int gap=1;
        while (gap<array.length){
            for (int i = 0; i < array.length; i+=gap*2) {
                int s1=i;
                int e1=s1+gap-i;
                if (e1>=array.length){
                    e1=array.length-1;
                }
                int s2=e1+1;
                if (s2>array.length){
                    s2=array.length-1;
                }
                int e2=s2+gap-1;
                if (e2>array.length){
                    e2=array.length-1;
                }
                mergerSort(array,s1,e2,s1);
            }
            gap*=2;
        }
    }
    public static void mergerSort(int[] array,int start,int end,int mid){
        int[] tmp=new int[end-start+1];
        int k=0;
        int s1=start;
        int e1=mid;
        int s2 = mid+1;
        int e2=end;
        while (s1<=e1&&s2<=e2){
            if (array[s1]<=array[s2]){
                tmp[k++]=array[s1++];
            }else {
                tmp[k++]=array[s2++];
            }
        }
        while (s1<=e1){
            tmp[k++]=array[s1++];
        }
        while (s2<=e2){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+start]=tmp[i];
        }
    }
    public static void countSort(int[] array){
        int maxVal=array[0];
        int minval=array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i]<minval){
                minval=array[i];
            }
            if (array[i]>maxVal){
                maxVal=array[i];
            }
        }
        int[] tmp=new int[maxVal-minval+1];
        for (int i = 0; i < tmp.length; i++) {
            int val=array[i];
            tmp[val-minval]++;
        }
        int index=0;
        for (int i = 0; i < tmp.length; i++) {
            while (tmp[i]!=0){
                array[index]=i+index;
                index++;
                tmp[i]--;
            }
        }
    }

}
